﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using MigratorCore;
using System.Threading;

namespace MigratorWizard
{
    public partial class Main : Form
    {
        private Migrator migrator;

        public Main()
        {
            InitializeComponent();
            migrator = new Migrator();
            actionList1.Main = this;
            providerList1.Main = this;
            welcome1.Main = this;
            details1.Main = this;
            progress1.Main = this;
            finished1.Main = this;
            progress.Maximum = 0;
            foreach (Control c in mainContent.Controls)
            {
                c.Top = 0;
                c.Left = 0;
                c.Visible = false;
                progress.Maximum++;
            }
            progress.Maximum--;
            CurrentStep = welcome1;
        }

        public Migrator Migrator
        {
            get { return migrator; }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            if (VistaControls.OSSupport.IsCompositionEnabled)
            {
                VistaControls.DWM.Margins m = new VistaControls.DWM.Margins(0, 0, 41, 0);
                VistaControls.DWM.DWMManager.EnableGlassFrame(this, m);
                VistaControls.DWM.GlassHelper.HandleFormMovementOnGlass(this, m);
                VistaControls.DWM.GlassHelper.HandleBackgroundPainting(this, m);
                VistaDWM.WTA_OPTIONS ops = new VistaDWM.WTA_OPTIONS();
                ops.Flags = VistaDWM.WTNCA_NODRAWCAPTION | VistaDWM.WTNCA_NODRAWICON;
                ops.Mask = VistaDWM.WTNCA_NODRAWCAPTION | VistaDWM.WTNCA_NODRAWICON;
                VistaDWM.SetWindowThemeAttribute(this.Handle, VistaDWM.WindowThemeAttributeType.WTA_NONCLIENT, ref ops, (uint)Marshal.SizeOf(typeof(VistaDWM.WTA_OPTIONS)));
                ShowIcon = false;
                minIcon.Visible = true;
                title.Visible = true;
                topPanel.BackColor = Color.FromName("ControlText");
                btnBack.Left = 0;
                progress.Left = 512;
            }
            else
            {
                ShowIcon = true;
                btnBack.Left = 10;
                progress.Left = 502;
                topPanel.BackColor = Color.White;
                minIcon.Visible = false;
                title.Visible = false;
            }
        }

        private void btnBack_MouseEnter(object sender, EventArgs e)
        {
            btnBack.Image = Properties.Resources.leftbackbuttonhover;
        }

        private void btnBack_MouseLeave(object sender, EventArgs e)
        {
            if (btnBack.Enabled)
                btnBack.Image = Properties.Resources.leftbackbuttonenabled;
            else
                btnBack.Image = Properties.Resources.leftbackbuttondisabled;
        }

        private void btnBack_MouseDown(object sender, MouseEventArgs e)
        {
            btnBack.Image = Properties.Resources.leftbackbuttonpressed;
        }

        private void bottomPanel_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawLine(new Pen(new SolidBrush(Color.FromArgb(223, 223, 223))), 0, 0, bottomPanel.Width, 0);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            migrator.Stop();
            this.Close();
            Application.Exit();
        }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                title.Text = Text;
            }
        }

        public bool EnableBack
        {
            get { return btnBack.Enabled; }
            set 
            { 
                if (value) btnBack.Image = Properties.Resources.leftbackbuttonenabled;
                else btnBack.Image = Properties.Resources.leftbackbuttondisabled;
                btnBack.Refresh();
                btnBack.Enabled = value;
            }
        }

        public int Progress
        {
            get { return progress.Value; }
            set { progress.Value = value; }
        }

        public bool EnableNext
        {
            get { return btnNext.Enabled; }
            set { btnNext.Enabled = value; }
        }

        public bool EnableCancel
        {
            get { return btnCancel.Enabled; }
            set { btnCancel.Enabled = value; }
        }

        public string CanelButtonText
        {
            get { return btnCancel.Text; }
            set { btnCancel.Text = value; }
        }

        private Control CurrentStep
        {
            get
            {
                foreach (Control c in mainContent.Controls)
                    if (c.Visible) return c;
                return null;
            }
            set
            {
                foreach (Control c in mainContent.Controls)
                    if (c == value) c.Show();
                    else c.Hide();
            }
        }

        private int CurrentStepIndex
        {
            get
            {
                return mainContent.Controls.IndexOf(CurrentStep);
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            if (!btnBack.Enabled) return;
            progress.Value--;
            CurrentStep = mainContent.Controls[CurrentStepIndex - 1];
        }

        public event NextClick NextClick;

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (!btnNext.Enabled) return;
            if (NextClick != null) NextClick(sender, e);
            progress.Value++;
            CurrentStep = mainContent.Controls[CurrentStepIndex + 1];
        }
    }

    public delegate void NextClick(object sender, EventArgs e);
}
